package cn.samler.project.guestbook.service.impl;

import cn.samler.project.guestbook.entity.User;
import cn.samler.project.guestbook.mapper.UserMapper;
import cn.samler.project.guestbook.model.common.Response;
import cn.samler.project.guestbook.model.request.UserLoginRequest;
import cn.samler.project.guestbook.model.request.UserRegisterRequest;
import cn.samler.project.guestbook.service.UserService;
import cn.samler.project.guestbook.util.ContextHolderUtil;
import cn.samler.project.guestbook.util.EncryptUtil;
import cn.samler.project.guestbook.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;
    private final UserUtil userUtil;

    public UserServiceImpl(UserMapper userMapper, UserUtil userUtil) {
        this.userMapper = userMapper;
        this.userUtil = userUtil;
    }

    @Override
    public Response login(UserLoginRequest request) {
        User user = userMapper.findByUsername(request.getUsername());
        if (user == null || !Objects.equals(user.getPassword(), EncryptUtil.getPasswordEncryptStr(request.getPassword()))) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("用户名或密码错误").build();
        }

        if (user.getStatus() != User.Status.NORMAL) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("账户已被锁定，无法登录").build();
        }

        return Response.builder()
                .ret(Response.Ret.SUCCESS)
                .msg("登录成功")
                .data(userUtil.getAccessToken(user))
                .build();
    }

    @Override
    public Response register(UserRegisterRequest request) {
        if (!Objects.equals(request.getPassword(), request.getConfirmPassword())) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("两次输入的密码不一致").build();
        }

        Response resp = EncryptUtil.checkStrength(request.getPassword());
        if (!resp.isSuccess()) {
            return resp;
        }

        User user = userMapper.findByUsername(request.getUsername());
        if (user != null) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("用户名已被占用").build();
        }

        user = new User();
        user.setUsername(request.getUsername());
        user.setPassword(EncryptUtil.getPasswordEncryptStr(request.getPassword()));
        user.setRole(User.Role.USER);
        user.setStatus(User.Status.NORMAL);

        if (userMapper.insert(user) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("注册成功").build();
        }

        log.error("Failed to register user: {}", request);
        return Response.builder().ret(Response.Ret.FAILURE).msg("注册失败").build();
    }

    @Override
    public Response getUserInfo() {
        User user = (User) ContextHolderUtil.getCurrentUser();
        user.setPassword(null);

        return Response.builder().ret(Response.Ret.SUCCESS).msg("获取成功").data(user).build();
    }

    @Override
    public Response lockUser(Integer id) {
        User user = userMapper.findById(id);
        if (user == null) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("用户不存在").build();
        }

        if (user.getStatus() == User.Status.LOCKED) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("该用户已是锁定状态").build();
        }

        user.setStatus(User.Status.LOCKED);
        if (userMapper.updateStatus(user) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("锁定成功").build();
        }

        log.error("Failed to lock user: {}", user);
        return Response.builder().ret(Response.Ret.FAILURE).msg("锁定失败").build();
    }

    @Override
    public Response unlockUser(Integer id) {
        User user = userMapper.findById(id);
        if (user == null) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("用户不存在").build();
        }

        if (user.getStatus() == User.Status.NORMAL) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("该用户已是正常状态").build();
        }

        user.setStatus(User.Status.NORMAL);
        if (userMapper.updateStatus(user) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("解锁成功").build();
        }

        log.error("Failed to unlock user: {}", user);
        return Response.builder().ret(Response.Ret.FAILURE).msg("解锁失败").build();
    }
}
